package com.codeczx.rockerviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by codeczx on 2017/7/21.
 */

public class RockerView1 extends View {

	private static final String TAG = "RockerView";
	private Paint backgroundPaint;
	private Paint rockerPaint;
	private int rockerX;
	private int rockerY;
	private int rockerRadius;
	private int defaultSize = 400;
	private int radius;
	private int radiusDiff;

	public RockerView1(Context context) {
		this(context, null);
	}

	public RockerView1(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RockerView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		backgroundPaint = new Paint();
		backgroundPaint.setColor(Color.BLUE);
		backgroundPaint.setStyle(Paint.Style.FILL);
		backgroundPaint.setAntiAlias(true);

		rockerPaint = new Paint();
		rockerPaint.setColor(Color.RED);
		rockerPaint.setStyle(Paint.Style.FILL);
		rockerPaint.setAntiAlias(true);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		radius = Math.min(w, h) / 2;
		rockerRadius = radius / 2;
		rockerX = radius;
		rockerY = radius;
		radiusDiff = radius - rockerRadius;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int widthMeasureSpec) {
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		int specMode = MeasureSpec.getMode(widthMeasureSpec);


		int width = 0;

		switch (specMode) {
			case MeasureSpec.EXACTLY:
				width = specSize;
				break;
			case MeasureSpec.AT_MOST:
				width = Math.min(specSize, defaultSize);
				break;
		}
		return width;
	}

	private int measureHeight(int heightMeasureSpec) {
		int specSize = MeasureSpec.getSize(heightMeasureSpec);
		int specMode = MeasureSpec.getMode(heightMeasureSpec);

		int height = 0;

		switch (specMode) {
			case MeasureSpec.EXACTLY:
				height = specSize;
				break;
			case MeasureSpec.AT_MOST:
				height = Math.min(specSize, defaultSize);
				break;
		}
		return height;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(radius, radius, radius, backgroundPaint);
		canvas.drawCircle(rockerX, rockerY, rockerRadius, rockerPaint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		switch (event.getAction()) {
			case MotionEvent.ACTION_MOVE:
				int tempX = x - radius;
				int tempY = y - radius;
				if (Math.pow(tempX, 2) + Math.pow(tempY, 2) > Math.pow(radiusDiff, 2)) {
					double a = Math.atan2(tempY, tempX);
					rockerX = radius + (int) ((radiusDiff) * Math.cos(a));
					rockerY = radius + (int) ((radiusDiff) * Math.sin(a));
				}
				else {
					rockerX = x;
					rockerY = y;
				}
				break;
			case MotionEvent.ACTION_UP:
				rockerX = radius;
				rockerY = radius;
				break;
		}
		invalidate();
		return true;
	}

}
